﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyRapChieuPhim.DTO;
using QuanLyRapChieuPhim.BUS;

namespace QuanLyRapChieuPhim.GUI
{
    public partial class ucsThemTTDatVe : UserControl
    {
        int Ma = -1;
        bool IsLoaded = false;
        TTDatVeDTO DTO;
        TTDatVeBUS BUS;

        public ucsThemTTDatVe()
        {
            InitializeComponent();
        }

        private void ucsThemTTDatVe_Load(object sender, EventArgs e)
        {
            IsLoaded = false;
            btnBanVe.Enabled = false;
            Ma = int.Parse(Tag.ToString());
            if (Ma > 0)
                btnBanVe.Enabled = true;
            LoadDefault();
            IsLoaded = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Ma = 0;
            lbThongBao.Visible = false;
            TTDatVeBUS BUS = new TTDatVeBUS();
            int MaVe = new VeBUS().GetIDFromSuatHangDay
                (
                    int.Parse(cbSuatChieu.SelectedValue.ToString()),
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)
                );
            if (new VeBUS().CheckTrong(int.Parse(cbSuatChieu.SelectedValue.ToString()),
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
            {
                lbThongBao.Visible = true;
                return;
            }
            int Gia = new VeBUS().GetFromId(MaVe).GiaVe;
            TTDatVeDTO DTO = new TTDatVeDTO
                (
                    Ma, int.Parse(cbTaiKhoan.SelectedValue.ToString()),
                    MaVe, Gia, DateTime.Now
                );
            if (BUS.Add(DTO))
            {
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

            LoadDefault();
        }

        private void btnCapNhap_Click(object sender, EventArgs e)
        {
            Ma = int.Parse(Tag.ToString());
            lbThongBao.Visible = false;
            TTDatVeBUS BUS = new TTDatVeBUS();
            int MaVe = new VeBUS().GetIDFromSuatHangDay
                (
                    int.Parse(cbSuatChieu.SelectedValue.ToString()),
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)
                );
            if (new VeBUS().CheckTrong(int.Parse(cbSuatChieu.SelectedValue.ToString()),
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
            {
                lbThongBao.Visible = true;
                return;
            }
            int Gia = new VeBUS().GetFromId(MaVe).GiaVe;
            TTDatVeDTO DTO = new TTDatVeDTO
                (
                    Ma, int.Parse(cbTaiKhoan.SelectedValue.ToString()),
                    MaVe, Gia, DateTime.Now
                );
            if (BUS.Update(DTO))
            {
                MessageBox.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Cập nhật thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

            LoadDefault();
        }

        private void btnDienLai_Click(object sender, EventArgs e)
        {
            LoadDefault();
        }

        private void cbSuatChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                int masuat = int.Parse(cbSuatChieu.SelectedValue.ToString());

                cbHangGhe.DataSource = new VeBUS().GetList_Hang(masuat);
                cbHangGhe.DisplayMember = "HangGhe";
                cbHangGhe.SelectedIndex = 0;

                cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                cbDayGhe.DisplayMember = "DayGhe";
                cbDayGhe.SelectedIndex = 0;
            }
        }

        private void cbHangGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                lbThongBao.Visible = false;
                int masuat = int.Parse(cbSuatChieu.SelectedValue.ToString());

                cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                cbDayGhe.DisplayMember = "DayGhe";
                cbDayGhe.SelectedIndex = 0;

                if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
                {
                    lbThongBao.Visible = true;
                    return;
                }
            }
        }

        private void btnBanVe_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bán vé này?", "Hệ thống", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                BUS = new TTDatVeBUS();
                BUS.BanVe(Ma);
                Ma = 0;
                MessageBox.Show("Bán vé thành công", "Hệ thống", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                return;
        }

        //========================================================================================

        void LoadDefault()
        {
            try
            {
                lbThongBao.Visible = false;
                BUS = new TTDatVeBUS();
                DTO = BUS.GetFromId(Ma);

                cbSuatChieu.DataSource = new SuatChieuBUS().GetList();
                cbSuatChieu.DisplayMember = "TenSuatChieu";
                cbSuatChieu.ValueMember = "MaSuatChieu";
                cbSuatChieu.SelectedValue = new SuatChieuBUS().GetIDFromIDTTDat(Ma);

                cbTaiKhoan.DataSource = new TaiKhoanBUS().GetList_QLKhachHang();
                cbTaiKhoan.DisplayMember = "TenDangNhap";
                cbTaiKhoan.ValueMember = "MaTaiKhoan";

                cbTaiKhoan.SelectedValue = new TaiKhoanBUS().GetIDFromIDTTDat(Ma);

                int masuat = int.Parse(cbSuatChieu.SelectedValue.ToString());

                VeDTO Ve_DTO = new VeBUS().GetFromId(DTO.MaVe);
                cbHangGhe.DataSource = new VeBUS().GetList_Hang(masuat);

                cbHangGhe.DisplayMember = "HangGhe";
                cbHangGhe.SelectedIndex = Ve_DTO.HangGhe-1;

                cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                cbDayGhe.DisplayMember = "DayGhe";
                cbDayGhe.SelectedIndex = Ve_DTO.DayGhe - 1;
            }
            catch (Exception)
            {
                lbThongBao.Visible = false;
                cbSuatChieu.DataSource = new SuatChieuBUS().GetList();
                cbSuatChieu.DisplayMember = "TenSuatChieu";
                cbSuatChieu.ValueMember = "MaSuatChieu";
                cbSuatChieu.SelectedIndex = 0;

                cbTaiKhoan.DataSource = new TaiKhoanBUS().GetList_QLKhachHang();
                cbTaiKhoan.DisplayMember = "TenDangNhap";
                cbTaiKhoan.ValueMember = "MaTaiKhoan";

                cbTaiKhoan.SelectedIndex = 0;

                int masuat = int.Parse(cbSuatChieu.SelectedValue.ToString());

                cbHangGhe.DataSource = new VeBUS().GetList_Hang(masuat);
                cbHangGhe.DisplayMember = "HangGhe";
                cbHangGhe.SelectedIndex = 0;

                cbDayGhe.DataSource = new VeBUS().GetList_Day(masuat);
                cbDayGhe.DisplayMember = "DayGhe";
                cbDayGhe.SelectedIndex = 0;
            }
        }

        private void cbDayGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoaded)
            {
                lbThongBao.Visible = false;
                int masuat = int.Parse(cbSuatChieu.SelectedValue.ToString());

                if (cbHangGhe.SelectedIndex >= 0)
                {
                    if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
                    {
                        lbThongBao.Visible = true;
                        return;
                    }
                }
                else if (cbHangGhe.SelectedIndex < 0)
                {
                    cbHangGhe.SelectedIndex = 0;
                    if (new VeBUS().CheckTrong(masuat,
                    int.Parse(cbHangGhe.Text), int.Parse(cbDayGhe.Text)) == 0)
                    {
                        lbThongBao.Visible = true;
                        return;
                    }
                }
            }
        }

    }
}
